import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrlDispatch = CommonConfig.planningUrl + '/v1'
// 列举车间详细计划
export function getPlantPlanList (keyword, pageNum, pageSize, searchBy) {
  const newUrl = `${baseUrlDispatch}/workshop_plannings/detailed`

  let reqData = {
    params: {
      keyword: keyword || undefined,
      pageNum,
      pageSize,
      searchBy: searchBy || undefined
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取车间
export function getWorkshops (plantId) {
  const newUrl = '/schema/v1/workshop_kv'
  const params = {
    plantId
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

// 通过车间获取工位
export function getStation (level, schemeId) {
  const newUrl = '/schema/v1/workcenter_no_page'

  let reqData = {
    params: {
      level,
      schemeId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举车间设备
export function getMachine (deviceType, workCenterId, workshopId) {
  const newUrl = `tpm/v1/workshops/${workshopId}/devices`

  let reqData = {
    params: {
      deviceType: deviceType || undefined,
      workCenterId: workCenterId || undefined
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举工单
export function getWorkOrders (processPlanningId, workCenterId, workshopId, workshopPlanningId) {
  const newUrl = `${baseUrlDispatch}/work_orders`

  let reqData = {
    params: {
      processPlanningId: processPlanningId || undefined,
      workCenterId: workCenterId || undefined,
      workshopId: workshopId || undefined,
      workshopPlanningId: workshopPlanningId || undefined
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 根据设备编号和设备名称获取设备列表
export function getDevicesByNumberName ({ assetNumber, deviceName, type, tenantId }) {
  const baseUrl = 'tpm/v1'

  const newUrl = type === 'assetNumber' ? `${baseUrl}/devices_by_assetnumber` : `${baseUrl}/devices/by_deviceName`
  let reqData = {
    params: {
      assetNumber, // 设备编号
      deviceName, // 设备名称
      tenantId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 根据车间和年月日查询排班
export function getShifts (workshopid, start, end) {
  const newUrl = `upms/v1/teamscheduling_by_workshopandmonth2`

  let reqData = {
    params: {
      workshopid: workshopid || workshopid,
      start: start || undefined,
      end: end || undefined
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 创建工单
export function addWorkorder (query) {
  const url = `${baseUrlDispatch}/process_plannings/${query.processPlanningId}/work_orders`

  return axiosInstance.post(url, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 删除工单
export function deleteWorkOrder (workOrderId) {
  const newUrl = `${baseUrlDispatch}/work_orders/${workOrderId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
